import { post } from '../../utils/fetch'
import { forecast,tzycOne, tzycTwo } from '../../utils/api'
import * as Libs from '../../utils/Libs'
import * as _ from 'lodash'

export const getTzycTwo = async (params) => {
  return post(tzycTwo(params), {})
}

export const getTzycFirst = async (params) => {
  return post(tzycOne(params), {})
}

export const getForecast = async (params) => {
  return post(forecast(params),{})
}

export const buildContents = (params) => {
  let allContents = []
  _.map(params, (v = {}) => {
    let fullSingle = {}
    let single = []
    single.push(v.key)
    //年份
    fullSingle.key = v.key
    const vContent = v.content || {}
    const yysr = vContent.yysr || {}
    const yysrTotal = (yysr.total || {})['bigSumMoney']
    single.push(Libs.numberUpperFormat(yysrTotal, false))
    //营业总收入
    fullSingle.yysrTotal = Libs.numberUpperFormat(yysrTotal, false)
    let yysrDetail = []
    _.map(yysr.xq || [], (v) => {
      single.push(Libs.numberUpperFormat(v.bigSumMoney, false))
      yysrDetail.push(Libs.numberUpperFormat(v.bigSumMoney, false))
    })
    //营业总收入明细
    fullSingle.yysrDetail = yysrDetail
    const yycb = vContent.yycb || {}
    const yycbTotal = (yycb.total || {})['bigSumMoney']
    single.push(Libs.numberUpperFormat(yycbTotal, false))
    //营业总成本
    fullSingle.yycbTotal = Libs.numberUpperFormat(yycbTotal,false)
    let yycbDetail = []
    _.map(yycb.xq || [], (v) => {
      single.push(Libs.numberUpperFormat(v.bigSumMoney,false))
      yycbDetail.push(Libs.numberUpperFormat(v.bigSumMoney,false))
    })
    fullSingle.yycbDetail = yycbDetail
    const yylr = vContent.yylr || {}
    const yylrTotal = (yylr.total || {})['bigSumMoney']
    single.push(Libs.numberUpperFormat(yylrTotal,false))
    fullSingle.yylrTotal = Libs.numberUpperFormat(yylrTotal,false)
    let yylrDetail = []
    _.map(yylr.xq || [], (v) => {
      single.push(Libs.numberUpperFormat(v.bigSumMoney,false))
      yylrDetail.push(Libs.numberUpperFormat(v.bigSumMoney,false))
    })
    fullSingle.yylrDetail = yylrDetail
    const lrze = vContent.lrze || {}
    const lrzeTotal = (lrze.total || {})['bigSumMoney']
    single.push(Libs.numberUpperFormat(lrzeTotal,false))
    fullSingle.lrzeTotal = Libs.numberUpperFormat(lrzeTotal,false)
    let lrzeDetail = []
    _.map(lrze.xq || [], (v) => {
      single.push(Libs.numberUpperFormat(v.bigSumMoney,false))
      lrzeDetail.push(Libs.numberUpperFormat(v.bigSumMoney,false))
    })
    fullSingle.lrzeDetail = lrzeDetail
    const jlr = vContent.jlr || {}
    const jlrTotal = (jlr.total || {})['bigSumMoney']
    single.push(Libs.numberUpperFormat(jlrTotal,false))
    fullSingle.jlrTotal = Libs.numberUpperFormat(jlrTotal,false)
    let jlrDetail = []
    _.map(jlr.xq || [], (v) => {
      single.push(Libs.numberUpperFormat(v.bigSumMoney,false))
      jlrDetail.push(Libs.numberUpperFormat(v.bigSumMoney,false))
    })
    fullSingle.jlrDetail = jlrDetail
    allContents.push(fullSingle)
  })
  return allContents
}

export const buildTitles = (params) => {
  let yyzsrTitles = []
  let yyzcbTitles = []
  let yylrTitles = []
  let lrzeTitles = []
  let jlrTitles = []
  const vContent = params.content || {}
  const yysr = vContent.yysr || {}
  _.map(yysr.xq || [], (v) => {
    yyzsrTitles.push(v.codeName)
  })
  const yycb = vContent.yycb || {}
  _.map(yycb.xq || [], (v) => {
    yyzcbTitles.push(v.codeName)
  })
  const yylr = vContent.yylr || {}
  _.map(yylr.xq || [], (v) => {
    yylrTitles.push(v.codeName)
  })
  const lrze = vContent.lrze || {}
  _.map(lrze.xq || [], (v) => {
    lrzeTitles.push(v.codeName)
  })
  const jlr = vContent.jlr || {}
  _.map(jlr.xq || [], (v) => {
    jlrTitles.push(v.codeName)
  })
  return {
    yyzsrTitles: yyzsrTitles,
    yyzcbTitles: yyzcbTitles,
    yylrTitles: yylrTitles,
    lrzeTitles: lrzeTitles,
    jlrTitles: jlrTitles
  }
}

export const geneTopData = (params = {}) => {
  const first = [params.trthjt, params.qzthjt, params.trjt, params.qzjt]
  const second = [params.trthret, params.qzthret, params.trret, params.qzret]
  return [
    first,
    second
  ]
}

export const secondGetTitles = (params = {}) => {
  const firstXjlr= params.xjlr || {}
  const firstXjlrTitles = (firstXjlr.details || {}).titles || []
  const firstXjzc = params.xjzc || {}
  const firstXjzcTitles = (firstXjzc.details || {}).titles || []
  return {
    xjlrTitles:firstXjlrTitles,
    xjzcTitles: firstXjzcTitles
  }
}

export const secondGetRight = (totalYear, singleD) => {
  const xjlr = singleD.xjlr || {}
  const xjlrTotal = xjlr.total || []
  const xjlrXqContent = (xjlr.details || {}).contents || []
  const xjzc = singleD.xjzc  || {}
  const xjzcTotal = xjzc.total || []
  const xjzcXqContent = (xjzc.details || []).contents || []
  const xmjll = singleD.xmjll || {}
  const xmjllTotal = xmjll.total || []
  const ljjxjll = singleD.ljjxjll || {}
  const ljjxjllTotal = ljjxjll.total || []
  const xmjzjthbz = singleD.xmjzjthbz || {}
  const xmjzjthbzTotal = xmjzjthbz.total || []
  const ljjxjllthbz = singleD.ljjxjllthbz || {}
  const ljjxjllthbzTotal = ljjxjllthbz.total || []

  let fullArr = []
  _.map(totalYear, (v, i) => {
    let single = {}
    single.year = v
    single.aTotal = xjlrTotal[i]
    single.aDetails = xjlrXqContent[i]
    single.bTotal = xjzcTotal[i]
    single.bDetails = xjzcXqContent[i]
    single.cTotal = xmjllTotal[i]
    single.dTotal = ljjxjllTotal[i]
    single.eTotal = xmjzjthbzTotal[i]
    single.fTotal = ljjxjllthbzTotal[i]
    fullArr.push(single)
  })
  return fullArr
}
